package com.kedsoft.docms.controller;

import com.kedsoft.docms.common.MessageResult;
import com.kedsoft.docms.entity.Group;
import com.kedsoft.docms.entity.Rule;
import com.kedsoft.docms.service.GroupService;
import com.kedsoft.docms.service.RuleService;
import com.kedsoft.docms.util.StringUtil;
import com.kedsoft.docms.vo.GroupVo;
import com.kedsoft.docms.vo.PageVo;
import com.kedsoft.docms.vo.Pagination;
import com.kedsoft.docms.vo.RuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2018-11-13
 */
@Controller
@RequestMapping("/group")
public class GroupController extends BaseController {

    @Autowired
    private GroupService groupService;

    @Autowired
    private RuleService ruleService;

    @RequestMapping(value = "index")
    public String index() {
        return "group/index";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Group> getPageList(Pagination<Group> pagination, @RequestParam(required = false) String name){
        Map<String, Object> query = new HashMap<>();
        query.put("name", name);
        groupService.pageList(pagination,query);
        PageVo<Group> pageVo = new PageVo<>(pagination.getItems(),pagination.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "form")
    public String form(){
        return "group/form";
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(GroupVo param){
        Group bean = new Group();
        bean.setName(param.getName());
        bean.setStatus(param.getStatus());
        bean.setCrtime(new Date());
        groupService.insert(bean);
        return MessageResult.getSuccess("添加数据成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(GroupVo param){
        Group bean = new Group();
        bean.setName(param.getName());
        bean.setStatus(param.getStatus());
        bean.setId(param.getId());
        bean.setUptime(new Date());
        groupService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        groupService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "remove")
    @ResponseBody
    public MessageResult deletes(@RequestParam String ids){
        String[] id = ids.split(",");
        for (String i: id) {
            Integer intId = Integer.parseInt(i);
            groupService.deleteById(intId);
        }
        return MessageResult.getSuccess("数据已删除");
    }

    @RequestMapping(value = "search")
    public String search(){
        return "group/search";
    }

    @RequestMapping(value = "rules")
    public String rules(@RequestParam(value = "id", defaultValue = "0") int id, Model model){
        Group bean = groupService.selectById(id);
        Map<String ,Object> map = new HashMap<>();
        map.put("status",1);
        List<Rule> rules = ruleService.selectByMap(map);
        List<RuleVo> authrule = new ArrayList<>();
        String[] qx = null;
        if(bean.getRules()!=null)
            qx = bean.getRules().split(",");
        for(Rule rule:rules){
            RuleVo rv = new RuleVo();
            rv.setId(rule.getId());
            rv.setName(rule.getName());
            rv.setTitle(rule.getTitle());
            rv.setCheck(0);
            if(StringUtil.in_array(qx,""+rule.getId())){
                rv.setCheck(1);
            }
            authrule.add(rv);
        }
        model.addAttribute("authrule",authrule);
        return "group/rules";
    }

    @RequestMapping(value = "setrules")
    @ResponseBody
    public MessageResult setrules(@RequestParam String id, @RequestParam String rules,@RequestParam String name, @RequestParam String myrule){
        Group bean = new Group();
        bean.setRules(rules);
        bean.setId(Integer.parseInt(id));
        bean.setUptime(new Date());
        groupService.updateById(bean);
        return MessageResult.getSuccess("修改数据成功");
    }
}

